var e34widgets = function() { };

/*******************************************************************************
 * Represents a hyperlink preceeded by an icon
 ******************************************************************************/
e34widgets.IconLink = function (id, img, text, opt_parent) {
  var template = "<a id='@id@' href='javascript:void(0);' class='iconlink'><img src='@img@' /> @text@</a>";
  var data = {
      "id": id,
      "img": img,
      "text": text
  };
  this.element_ = e34.renderTemplate(template, data).getElement();
  if (opt_parent) {
    e34.$(opt_parent).appendChildren(this.element_);
  }
};

e34widgets.IconLink.prototype = new e34(null, false);

/*******************************************************************************
 * Represents a select drop down list
 ******************************************************************************/
e34widgets.DropDown = function(id, opt_parent) {
  var select_template = "<select id='@id@'></select>";
  this.option_template_ = "<option value=@value@>@text@</option>";
  this.num_options_ = 0;
  this.onempty_handler_ = null;
  this.onitems_handler_ = null;
  
  //Render the select template
  this.element_ = e34.renderTemplate(select_template, { 
    "id": id 
  }).getElement();
  
  //Attach the template to a parent if specified
  if (opt_parent) {
    e34.$(opt_parent).appendChildren(this.element_);
  }
};

e34widgets.DropDown.prototype = new e34(null, false);

e34widgets.DropDown.prototype.checkOptionsHandlers_ = function() {
  if (this.num_options_ == 1 && this.onitems_handler_) {
    this.onitems_handler_(this);
  } else if (this.num_options_ == 0 && this.onempty_handler_) {
    this.onempty_handler_(this);
  }
  return this;
};

e34widgets.DropDown.prototype.addOption = function(value, text) {
  var option = e34.renderTemplate(this.option_template_, {
    "value": value,
    "text": text
  });
  LOG("option", option);
  this.appendChildren(option);
  this.num_options_++;
  this.checkOptionsHandlers_();
  return this;
};

e34widgets.DropDown.prototype.clearOptions = function() {
  this.removeAllChildren();
  this.num_options_ = 0;
  this.checkOptionsHandlers_();
  return this;
};

e34widgets.DropDown.prototype.addOnItemsHandler = function(callback) {
  if (e34.isFunction(callback)) {
    this.onitems_handler_ = callback;
  }
  this.checkOptionsHandlers_();
  return this;
};

e34widgets.DropDown.prototype.addOnEmptyHandler = function(callback) {
  if (e34.isFunction(callback)) {
    this.onempty_handler_ = callback;
  }
  this.checkOptionsHandlers_();
  return this;
};

e34widgets.DropDown.prototype.getSelectedText = function() {
  return this.element_[this.element_.selectedIndex].innerHTML;
};

e34widgets.DropDown.prototype.select = function(id) {
  for (var i = 0, option; option = this.element_.childNodes[i]; i++) {
    if (option.value == id) {
      option.selected = "selected";
    }
  }
  return this;
};

/*******************************************************************************
 * Represents a content box overlaid on top of the current page
 ******************************************************************************/
e34widgets.Overlay = function(id, width, top, opt_parent) {
  var shadow_template = "<div id='@id@'></div>";
  var content_template = "<div></div>";
  
  this.element_ = e34.renderTemplate(shadow_template, { 
    "id": id 
  }).getElement();
  
  //Attach the template to a parent if specified
  if (opt_parent) {
    e34.$(opt_parent).appendChildren(this.element_);
  }
  
  this.setStylePosition("absolute")
      .setStyleLeft("50%")
      .setWidth(width)
      .setStyleTop(top);
  
  this.content_element_ = e34.renderTemplate(content_template, {})
      .setParent(this.element_)
      .setStylePosition("relative");
};

e34widgets.Overlay.prototype = new e34(null, false);

e34widgets.Overlay.prototype.setContent = function(content) {
  e34.$(content).setParent(this.content_element_);
  return this;
};

e34widgets.Overlay.prototype.setWidth = function(w) {
  this.setStyleWidth(w);
  var margin_left = "-" + Math.round(this.getDimensions().width / 2) + "px";
  this.setStyleMarginLeft(margin_left);
  return this;
};

e34widgets.Overlay.prototype.addContentClass = function(classname) {
  this.content_element_.addClass(classname);
  return this;
};
